#include "common.h"
#include "windows.h"
#include "cuda_runtime.h"
#include "device_launch_parameters.h"


double getCpuClock(void)
{
	LARGE_INTEGER freq;
	LARGE_INTEGER count;
	QueryPerformanceFrequency(&freq);

	QueryPerformanceCounter(&count);

	//cout << "freq:" << freq.QuadPart << ",count:" << count.QuadPart << endl;

	return ((double)count.QuadPart) / ((double)freq.QuadPart);
}

int init_cuda(int width, int height, unsigned char * data, unsigned char ** dev_buffOut, int * blockNum, int *pitch)
{
	*pitch = ((width + 7) >> 3) << 3;
	cudaError_t retError = cudaMalloc(dev_buffOut,1024*1024*BLOCK_NUM_TO_ALLOC);
	if (retError != 0)
	{
		return retError;
	}

	*blockNum = BLOCK_NUM_TO_ALLOC;
	if (data != NULL)
	{
		cudaMemcpy2D(*dev_buffOut, *pitch, data, width, width, height, cudaMemcpyHostToDevice);
	}
	return 0;
}

void deinit_cuda(unsigned char * dev_buff, int * blockNum, int *pitch)
{
	cudaFree(dev_buff);
	*blockNum = 0;
	*pitch = 0;
}